package com.tianbao54.grpc.service;

import com.tianbao54.grpc.model.StreamRequest;
import com.tianbao54.grpc.model.StreamResponse;
import com.tianbao54.grpc.model.TalkServiceGrpc;
import io.grpc.stub.StreamObserver;

import java.util.UUID;

public class MyServiceImpl extends TalkServiceGrpc.TalkServiceImplBase {

    @Override
    public StreamObserver<StreamRequest> biTalk(StreamObserver<StreamResponse> responseObserver) {

        return new StreamObserver<StreamRequest>() {
            @Override
            public void onNext(StreamRequest value) {
                System.out.println(value.getRequestInfo());
                responseObserver.onNext(StreamResponse.newBuilder().setResponseInfo(UUID.randomUUID().toString()).build());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());
            }

            @Override
            public void onCompleted() {
                responseObserver.onCompleted();
            }
        };
    }
}